﻿using System.Data;
using Microsoft.SharePoint;

namespace QueryDemo.ResultProviders
{
	/// <summary>
	/// Use of the SPQuery object on an SPList.
	/// </summary>
	public class SPQueryResultProvider : BaseResultProvider
	{
		private SPList _list;
		private SPQuery _query;

		/// <summary>
		/// Sets properties related to use of the query within the web part.
		/// </summary>
		public SPQueryResultProvider()
		{
			TypeOfQuery = QueryType.SPQuery;
			QueryName = "SPQuery";
		}

		public override bool CrossSiteCapable
		{
			get { return false; }
		}

		public override void Init()
		{
			_list = SPContext.Current.Web.Lists[ListTitle];
			_query = new SPQuery
			         	{
			         		ViewFields = GetViewFieldCaml(),
							Query = CamlQuery,
							RowLimit = (uint)RowLimit
			         	};
		}

		public override DataTable ExecuteQuery()
		{
			SPListItemCollection resultsCollection = _list.GetItems(_query);
			return resultsCollection.GetDataTable();
		}
	}
}
